package servicesImp.expertInfo;


import StaticFinal.CrudType;
import base.IBaseMapperWithoutBLOBs;
import exception.WTException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.dictionary.CertificateTypeServices;
import services.dictionary.ItemTypeServices;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ExpertCertificateServices;
import services.expertInfo.LeaderCheckServices;
import wt.dao.expertInfo.ExpertCertificateMapper;
import wt.entity.dictionary.CertificateType;
import wt.entity.dictionary.ItemType;
import wt.entity.expertInfo.ExpertCertificate;
import wt.entity.expertInfo.ExpertCertificateExample;
import wt.staticData.TableName;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class ExpertCertificateExpertInfoServicesImp extends BaseExpertInfoServicesImp<ExpertCertificate, ExpertCertificateExample> implements ExpertCertificateServices {
    @Autowired
    ExpertCertificateMapper expertCertificateMapper;
    @Autowired
    LeaderCheckServices leaderCheckServices;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;
    @Autowired
    ExpertBaseServices expertBaseServices;

    @Override
    public IBaseMapperWithoutBLOBs<ExpertCertificate, ExpertCertificateExample> getMapper() {

        return this.expertCertificateMapper;
    }

    @Override
    public String getTableName() {
        return TableName.EMS_EXPERT_CERTIFICATE;
    }


    @Override
    public ExpertCertificate getEntity() {
        return new ExpertCertificate();
    }

    @Override
    public ExpertCertificateExample getRecordExample() {
        return new ExpertCertificateExample();
    }

    @Override
    public LeaderCheckServices getLeaderCheckServices() {
        return leaderCheckServices;
    }

    @Override
    public ExpertBaseServices getExpertBaseServices() {
        return expertBaseServices;
    }

    @Override
    @Transactional
    public void adminInsertSelective(ExpertCertificate expertCertificate) throws Exception {
        if (isExistUnauditedRecord(expertCertificate.getExpertId())) {
            throw new WTException(WTException.FOUND_NOT_CHECK_DATA);
        }
        super.adminInsertSelective(expertCertificate);
    }


    @Override
    protected void recordProcess(ExpertCertificate expertCertificate) throws Exception {
        expertCertificate.setCertificateType(certificateTypeServices.selectByPrimaryKey(expertCertificate.getCertificateTypeId()));
    }


    public void lazyLoadCertificateType(List<ExpertCertificate> expertCertificateList) throws Exception {
        if (expertCertificateList == null) {
            return;
        }
        Map<String, CertificateType> map = new HashMap<String, CertificateType>();
        for (ExpertCertificate expertCertificate : expertCertificateList) {
            if (!map.containsKey(expertCertificate.getCertificateTypeId())) {
                map.put(expertCertificate.getCertificateTypeId(), certificateTypeServices.selectByPrimaryKey(expertCertificate.getCertificateTypeId()));
            }
            expertCertificate.setCertificateType(map.get(expertCertificate.getCertificateTypeId()));
        }

    }


    public boolean isExistCertificate(String expertId, String itemId) throws Exception {
        ExpertCertificateExample expertCertificateExample = new ExpertCertificateExample();
        expertCertificateExample.createCriteria().andExpertIdEqualTo(expertId).andItemIdEqualTo(itemId);
        int count = countByExample(expertCertificateExample);
        if (count == 0) {
            return false;
        } else {
            return true;
        }
    }

    public ExpertCertificate adminSelect(String expertId, String itemId) throws Exception {

        ExpertCertificateExample expertCertificateExample = new ExpertCertificateExample();
        expertCertificateExample.createCriteria().andExpertIdEqualTo(expertId).andItemIdEqualTo(itemId).andCrudTypeNotEqualTo(CrudType.insert);
        List<ExpertCertificate> expertCertificateList = selectByExample(expertCertificateExample);
        if (expertCertificateList.isEmpty()) {
            return null;
        } else {
            return expertCertificateList.get(0);
        }
    }


    public ExpertCertificate adminSelectByItemNum(String expertId, String itemNum) throws Exception {
        ItemType itemType = itemTypeServices.selectByItemNum(itemNum);
        if (itemType == null) {
            return null;
        } else {
            return adminSelect(expertId, itemType.getId());
        }
    }
}
